// Program to periodically open and close "display" windows used by the TC daemon

#include <iostream>
#include <unistd.h> // standard unix functions, e.g., alarm()
#include <signal.h> // signal name macros, and the signal() prototype

using namespace std;

int ALARM_T;

void catch_alarm(int sig_num){
  system("./windows_close");
  system("./windows_open");

  // Set next alarm
  alarm(ALARM_T);
}

void catch_kill(int sig_num){
  system("./windows_close");
  cout << "TCdisplay cancelled by user." << endl;
  exit(1);
}

int main(int argc, char** argv)
{
  if (argc != 2){
    cerr << "Usage: TCdisplay delay" << endl
         << "  delay: period (in sec.) between open/close operations" << endl;
    return 1;
  }

  ALARM_T = atoi(argv[1]);

  if (ALARM_T < 10){
    cerr << "TCdisplay error: minimum allowed delay is 10 seconds" << endl;
    return 1;
  }

  // Set a signal handler for ALRM signals
  signal(SIGALRM, catch_alarm);

  // Set a signal handler for KILL signals
  signal(SIGINT, catch_kill);

  // Call catch_alarm
  catch_alarm(SIGALRM);

  while (1){
    pause();  
  }

  return 0;
}
